<template>
	<view>
		<view class="detail-box">
			<view class="cu-form-group">
				<view class="title">商品名称：</view>
				<input class="inpt" name="input" :value="NowSelectGoods.GoodsName"></input>
				<!-- <button class="cu-btn round stop text-white" @tap="gogoodlist">查询</button> -->
			</view>
			<view class="cu-form-group">
				<view class="title">编号：</view>
				<input class="inpt" name="input" disabled="true" v-model="NowSelectGoods.GoodsCode"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">单位：</view>
				<view class="inpt1"><input type="text" placeholder="点击获取" :value="NowSelectGoods.UnitName" disabled="true"
					 style="text-align: left;" data-target="UnitNameModal" @tap="showModal" />
					<view class="cu-modal" :class="modalName=='UnitNameModal'?'show':''">
						<view class="cu-dialog">
							<view class="cu-bar bg-white justify-end">
								<view class="content">请选择单位</view>
								<view class="action" @tap="hideModal">
									<text class="cuIcon-close text-red"></text>
								</view>
							</view>
							<view class="padding-xl">
								<view class="searchStore">
									<input placeholder="请输入名称" v-model="SearchUnitName" @input="filterUniName"/>
									<button @tap="searchUnitName">搜索</button>
								</view>
								<view class="StoreListWrap">
									<view v-for="(item,index) in TempUnitNameList" :key="index" @tap="selectUnitName(index)">{{item.UnitName}}</view>
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
			<view class="cu-form-group">
				<view class="title">单价：</view>
				<view class="inpt1"><input type="text" placeholder="点击获取" v-model="NowSelectGoods.Price"
					 style="text-align: left;" />
					<view class="cu-modal" :class="modalName=='PriceModal'?'show':''">
						<view class="cu-dialog">
							<view class="cu-bar bg-white justify-end">
								<view class="content">请选择单价</view>
								<view class="action" @tap="hideModal">
									<text class="cuIcon-close text-red"></text>
								</view>
							</view>
							<view class="padding-xl">
								<view class="StoreListWrap">
									<view v-for="(item,index) in PriceList" :key="index" @tap="selectPriceName(index)">{{item.ShowValue}}</view>
								</view>
							</view>
						</view>
					</view>
				</view>
			    <view><button class="priceSear"  data-target="PriceModal" @tap="showModal">查询</button></view>
			</view>
			<view class="cu-form-group">
				<view class="title">数量：</view>
				<input class="inpt" name="input" placeholder="点击修改" v-model="NowSelectGoods.Qty" ></input>
			</view>
			<view class="cu-form-group" v-if="IsCx==4">
				<view class="title">进价：</view>
				<input class="inpt" name="input" disabled v-model="InPrice" ></input>
			</view>
			<view class="cu-form-group">
				<view class="title">折扣：</view>
				<input class="inpt" name="input" placeholder="点击修改" v-model="NowSelectGoods.Discount" ></input>
			</view>
			<view class="cu-form-group">
				<view class="title">规格：</view>
				<input class="inpt" name="input" placeholder="点击修改" disabled="true" v-model="NowSelectGoods.Spec" ></input>
			</view>
			<view class="cu-form-group">
				<view class="title">税率：</view>
				<input class="inpt" name="input" placeholder="点击修改" v-model="NowSelectGoods.Tax" ></input>
			</view>
			<view class="cu-form-group">
				<view class="title">库存：</view>
				<input class="inpt" name="input" disabled="true" :value="NowSelectGoods.stockqty"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">仓库：</view>
				<view class="inpt1"><input type="text" placeholder="点击获取" :value="SelectStoreObj.StoreName" disabled="true"
					 style="text-align: left;" data-target="StoreModal" @tap="showModal" />
					<view class="cu-modal" :class="modalName=='StoreModal'?'show':''">
						<view class="cu-dialog">
							<view class="cu-bar bg-white justify-end">
								<view class="content">请选择仓库</view>
								<view class="action" @tap="hideModal">
									<text class="cuIcon-close text-red"></text>
								</view>
							</view>
							<view class="padding-xl">
								<view class="searchStore">
									<input placeholder="请输入仓库名称" v-model="SearchStoreName" @input="filterStore"/>
									<button @tap="searchStore">搜索</button>
								</view>
								<view class="StoreListWrap">
									<view v-for="(item,index) in TempStoreList" :key="item.StoreID" @tap="selectStore(index)">{{item.StoreName}}</view>
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
			<view class="cu-form-group">
				<view class="title">备注：</view>
				<input class="inpt" name="input" placeholder="点击修改" v-model="NowSelectGoods.Remark" ></input>
			</view>
		</view>
		<view class="cu-form-group">
			<input class="inpt" name="input" disabled="true"></input>
			<button class="cu-btn  stop text-white" @tap="addSelectGoods">提交修改</button>
		</view>
		<uni-calendar ref="calendar1" :insert="false" @confirm="confirm1" />
		<uni-calendar ref="calendar2" :insert="false" @confirm="confirm2" />
	</view>
</template>
<script>
	import * as Api from '@/syApis/syAddOrder/syaddApis.js';
	import uniCalendar from '@/components/uni-calendar/uni-calendar.vue';
	export default {
		data() {
			return {
				//选择的商品
				NowSelectGoods: {},
				//添加的备注
				GoodsRemark: "",
				//商品库存
				GoodsStorm: 0,
				//商品税率
				Tax: 0,
				//商品
				GoodsQty: 0,
				//商品折扣
				GoodsDiscount: "",
				//仓库ID
				StoreID: "",
				//当前商品在原商品数组对应的位置
				index2:-1,
				picker2: [],
				// SelectStockNum:"",
				StoreList:[],
				startTime:"",
				endTime:"",
				//当前选择的仓库条目
				SelectStoreObj: {
					StoreName: "",
					StoreID: ""
				},
				SearchStoreName:"",
				modalName:null,
				//多单位列表
				UnitNameList:[],
				//多单位搜索
				SearchUnitName:"",
				//价格跟踪
				PriceList:[],
				//价格跟踪搜索
				SearchPrice:"",
				TempStoreList:[],
				TempUnitNameList:[],
				BillName:"",
				isNumQtStorm:false,
				isSet:false,
				isTipPrice:false,
				ClientID:"",
				InPrice:0,
				isBatchManger:true
			}
		},
		created() {
			this.GetStoreMessage();
		},
		onLoad(options) {
			console.log(options);
			let goodData = uni.getStorageSync("ChangeGoodData","");
			if(goodData!=""){
				this.NowSelectGoods =JSON.parse(goodData) ;
			}
			this.StoreID = options.StoreID;
			this.index = options.selectIndex;
			this.IsCx = options.IsCx;
			this.BillName = options.BillName;
			this.ClientID = options.ClientID;
			// this.isSet = options.isSet;
			this.Get_GoodsUnitMessage();
			var that = this;
			let SystemSettingInfo = uni.getStorageSync("SystemSettingInfo", ""); //存储系统设置
			SystemSettingInfo = JSON.parse(SystemSettingInfo);
			SystemSettingInfo.forEach((item, index) => {
				if (item.SystemName == '是否允许负库存出库') {
					if (item.Value == "1") {
						that.isNumQtStorm = true;
					}
					// console.log(item.Value);
				}
				if (item.SystemName == '售价低于进价提示') {
					if (item.Value == "1") {
						that.isTipPrice = true;
					}
					// console.log(item.Value);
				}
				if (item.SystemName == '开启批号管理') {
					if (item.Value != "1") {
						that.isBatchManger = false;
					}
					// console.log(item.Value);
				}
			});
		},
		components: {
			uniCalendar
		},
		methods: {
			filterStore(e){
				let value = e.detail.value;
				this.TempStoreList = [];
				var that = this;
				this.StoreList.forEach((item,index)=>{
					if(item.StoreName.indexOf(value)>-1){
						that.TempStoreList.push(item);
					}
				});
				if(that.TempStoreList.length==0){
					Api.showTip("查无数据");
				}
			},
			filterUniName(e){
				let value = e.detail.value;
				this.TempUnitNameList = [];
				var that = this;
				this.UnitNameList.forEach((item,index)=>{
					if(item.UnitName.indexOf(value)>-1){
						that.TempUnitNameList.push(item);
					}
				});
				if(that.TempUnitNameList.length==0){
					Api.showTip("查无数据");
				}
			},
			//多单位搜索
			searchUnitName(){
				let Filter = "";
				if(this.SearchUnitName!=''){
					Filter = "UnitName like '%"+this.SearchUnitName+"%'";
					this.Get_GoodsUnitMessage(Filter);
				}else{
					this.Get_GoodsUnitMessage();
				}
			},
			//多单位点击
			selectUnitName(index){
				 this.NowSelectGoods.UnitName = this.TempUnitNameList[index].UnitName;
				 this.NowSelectGoods.urate = this.TempUnitNameList[index].urate;
				 this.NowSelectGoods.UnitID = this.TempUnitNameList[index].UnitID;
				 this.XH_Get_PriceFollowMessage(this.TempUnitNameList[index]);
				 this.modalName = null;
			},
			//价格跟踪搜索
			searchPrice(){
				let Filter = "";
				if(this.SearchPrice!=''){
					Filter = "ShowValue like '%"+this.SearchPrice+"%'";
					this.XH_Get_PriceFollowMessage(this.NowSelectGoods,Filter);
				}else{
					this.XH_Get_PriceFollowMessage(this.NowSelectGoods);
				}
			},
			//多单位点击
			selectPriceName(index){
				 this.NowSelectGoods.Price = this.PriceList[index].Price;
				 this.NowSelectGoods.PFPrice = this.PriceList[index].Price;
				 this.modalName = null;
			},
			//价格跟踪
			XH_Get_PriceFollowMessage(priceItem,Filter=""){
				let data = {
					GoodsID:priceItem.GoodsID,
					UnitID:priceItem.UnitID,
					BillName:this.BillName,
					URate:priceItem.urate,
					ClientID:this.ClientID
				}
				var that = this;
				Api.XH_Get_PriceFollowMessage(data,function(res){
					if(res.state){
						if(res.data.ResultMsg!=""){
							that.PriceList = res.data.ResultMsg;
							res.data.ResultMsg.forEach((item,index)=>{
								  if(item.PriceType == '进价'){
									  that.InPrice = item.Price;
								  }
							 });
							if(!that.NowSelectGoods.isSet){
								that.NowSelectGoods.Price = that.PriceList[0].Price;
							}
						}else if(res.data.ErrorMsg=='获取登录信息失败'){
							Api.showTip(res.data.ErrorMsg);
						}else{
							Api.showTip(res.data.ErrorMsg);
						}
					}else{
						
					}
				});
			},
			DiscountPrice(){
				this.NowSelectGoods.DiscountPrice = parseFloat(this.NowSelectGoods.Price)  * (parseFloat(this.NowSelectGoods.Discount) / 100) * (1 + parseFloat(
					this.NowSelectGoods.Tax) / 100);
			},
			//计算总价
			Amount(){
				this.NowSelectGoods.Amount = parseFloat(this.NowSelectGoods.Price) * parseFloat(this.NowSelectGoods.Qty) * (parseFloat(this.NowSelectGoods.Discount) / 100) * (1 + parseFloat(
					this.NowSelectGoods.Tax) / 100);
					// return  PFPrice;
			},
			//查询商品多单位
			Get_GoodsUnitMessage(Filter=''){
				let data = {
					GoodsID:this.NowSelectGoods.GoodsID,
					Filter:Filter
				}
				var that = this;
				Api.Get_GoodsUnitMessage(data,function(res){
					if(res.state){
						if(res.data.ResultMsg!=""){
							if(Filter!=""){
								// that.UnitNameList = res.data.ResultMsg;
								that.TempUnitNameList = res.data.ResultMsg;
							}else{
								that.UnitNameList = res.data.ResultMsg;
								that.TempUnitNameList = res.data.ResultMsg;
							}
							
							if(that.UnitNameList.length>0){
								res.data.ResultMsg.forEach((item, index) => {
									if (item.UnitName == that.NowSelectGoods.UnitName) {
										that.XH_Get_PriceFollowMessage(item);
									}
								})
								// that.XH_Get_PriceFollowMessage(that.UnitNameList[0]);
							}
						}else if(res.data.ErrorMsg=='获取登录信息失败'){
							Api.showTip(res.data.ErrorMsg);
						}else{
							Api.showTip(res.data.ErrorMsg);
						}
					}else{
						
					}
				});
			},
			//搜索仓库
			searchStore() {
				if (this.SearchStoreName != '') {
					let Filter = "StoreName like '%" + this.SearchStoreName + "%'";
					this.GetStoreMessage(Filter);
				} else {
					this.GetStoreMessage();
				}
			},
			showModal(e) {
				console.log(1111)
				this.modalName = e.currentTarget.dataset.target
			},
			//选择仓库
			selectStore(index) {
				this.SelectStoreObj = this.StoreList[index];
				this.NowSelectGoods.StoreID = this.SelectStoreObj.StoreID;
				this.modalName = null;
				this.SelectStockNum();
			},
			hideModal(){
				this.modalName = null;
			},
			open1() {
				this.$refs.calendar1.open();
			},
			confirm1(e) {
				// var fulldate = e.detail.fulldate
				// console.log(e.detail.fulldate);
				console.log(e)
				this.startTime = e.fulldate;
				this.NowSelectGoods.startTime = e.fulldate;
			},
			open2() {
				this.$refs.calendar2.open();
			},
			confirm2(e) {
				// var fulldate = e.detail.fulldate
				// console.log(e.detail.fulldate);
				console.log(e)
				this.endTime = e.fulldate;
				this.NowSelectGoods.EndTime = e.fulldate;
			},
			//获取仓库
			GetStoreMessage(Filter="") {
				let data = {
					Filter: Filter
				}
				var that = this;
				Api.GetStoreMessage(data, function(res) {
					if (res.state) {
						if (res.data.ErrorMsg == "获取登录信息失败") {
							Api.showTip("登录超时");
						} else if (res.data.ErrorMsg != "") {
							Api.showTip(res.data.ErrorMsg);
						} else {
							let responseData = res.data.ResultMsg;
							
							// that.NowSelectGoods.StoreID = res.data.ResultMsg[0].BranchID;
							responseData.forEach((item, index) => {
								if(that.NowSelectGoods.StoreID == item.StoreID){
									that.SelectStoreObj = item;
								}
							});
							if(Filter==""){
								 that.StoreList = res.data.ResultMsg;
								 that.TempStoreList = res.data.ResultMsg;
							}else{
								 that.TempStoreList = res.data.ResultMsg;
							}
						}
					} else {
			
					}
				});
			},
			PickerChange2(e) {
				console.log(e)
				if(e.detail.value==-1){
					this.index2 = 0;
				}else{
					this.index2 = e.detail.value;
				}
			
				this.NowSelectGoods.StoreID = this.StoreList[this.index2].StoreID;
			},
			//折扣改变事件
			changeZk(e){
				console.log(e);
			},
			//获取当前选择的商品
			addSelectGoods(){
			 //    this.NowSelectGoods.Qty = this.GoodsQty;
				// this.NowSelectGoods.Discount = this.Discount;
			 //    this.NowSelectGoods.Tax = this.Tax;
				// this.NowSelectGoods.Remark = this.GoodsRemark;
				// this.NowSelectGoods.GoodsStorm = this.GoodsStorm;
				this.DiscountPrice();
				this.Amount();
				console.log(this.NowSelectGoods);
				if(this.NowSelectGoods.Qty<=0){
					Api.showTip("商品数量不能少于一件");
					return;
				}
				if(this.NowSelectGoods.Qty>this.NowSelectGoods.stockqty && !this.isNumQtStorm && this.IsCx == 4){
					Api.showTip("当前库存不足");
					return;
				}
				if(this.SelectStoreObj.StoreID==''){
					Api.showTip("请先选择仓库");
					return;
				}
				let DiscountStr = this.NowSelectGoods.Discount+'';
				if(DiscountStr.indexOf(".")>-1){
					Api.showTip("折扣只能是整数");
					return;
				}
				let TaxStr = this.NowSelectGoods.Tax+'';
				// console.log(this.NowSelectGoods.Tax+''.indexOf("."));
				if(TaxStr.indexOf(".")>-1){
					Api.showTip("税率只能是整数");
					return;
				}
				var that = this;
				if (this.IsCx == 20) {
					if (parseFloat(this.NowSelectGoods.Price ) <parseFloat(this.InPrice)  && this.isTipPrice) {
						uni.showModal({
							title: '提示',
							content: "售价低于进价是否继续修改",
							success: function(res) {
								if (res.confirm) {
									// _this.NowSelectGoods.GoodsStorm = this.GoodsStorm;
									// console.log(this.NowSelectGoods);
									let data = {};
									that.DiscountPrice();
									that.Amount();
									data.index = that.index;
									data.NewGoodInfo = JSON.stringify(that.NowSelectGoods);
									that.$eventHub.$emit('fire', data);
									uni.navigateBack({
										delta: 1
									});
									// let data = {};
									// data.index = this.index;
									// data.NewGoodInfo = JSON.stringify(this.NowSelectGoods);
									// this.$eventHub.$emit('fire', data);
									// uni.navigateBack({
									// 	delta:1
									// })
								} else if (res.cancel) {
				
								}
							}
						});
					}else{
						let data = {};
						that.DiscountPrice();
						that.Amount();
						data.index = that.index;
						data.NewGoodInfo = JSON.stringify(that.NowSelectGoods);
						that.$eventHub.$emit('fire', data);
						uni.navigateBack({
							delta: 1
						});
					}
				} else {
					let data = {};
					that.DiscountPrice();
					that.Amount();
					data.index = that.index;
					data.NewGoodInfo = JSON.stringify(that.NowSelectGoods);
					that.$eventHub.$emit('fire', data);
					uni.navigateBack({
						delta: 1
					});
				}
				
			},
			DiscountPrice(){
				console.log(this.NowSelectGoods.Price);
				console.log(this.NowSelectGoods.Discount);
				
				this.NowSelectGoods.DiscountPrice = parseFloat(this.NowSelectGoods.Price)  * (parseFloat(this.NowSelectGoods.Discount) / 100) * (1 + parseFloat(
					this.NowSelectGoods.Tax) / 100);
				console.log(this.NowSelectGoods.DiscountPrice);
			},
			//计算总价
			Amount(){
				this.NowSelectGoods.Amount = parseFloat(this.NowSelectGoods.Price) * parseFloat(this.NowSelectGoods.Qty) * (parseFloat(this.NowSelectGoods.Discount) / 100) * (1 + parseFloat(
					this.NowSelectGoods.Tax) / 100);
					// return  PFPrice;
			},
			//获取当前选择的商品的库存
			SelectStockNum() {
				let data = {
					StoreID: this.SelectStoreObj.StoreID,
					GoodsID: this.NowSelectGoods.GoodsID,
					BillName: this.BillName
				};
				var _this = this;
				Api.SelectStockNum(data, (res) => {
					if (res.data.ErrorMsg == "获取登录信息失败") {

					} else if (res.data.ErrorMsg != "") {
						Api.showTip(res.data.ErrorMsg);
					} else {
						// _this.GoodsStorm = res.data.ResultMsg;
						_this.NowSelectGoods.stockqty = res.data.ResultMsg;
					}
				});
			},
			//计算折扣后商品价格
			computPrice(item){
				item.PFPrice = item.PFPrice*item.Qty*(item.Discount/100)*(1+item.Tax/100);
				return item;
			}
		}
	}
</script>

<style >
	.stop {
		background: rgba(124, 170, 216,1) !important;
	}
	.inpt1 {
		flex: 1;
		text-align: left;
		align-items: center;
		display: flex;
	}
	/* 弹框样式 */
	.padding-xl .searchStore{
		display: flex;
		width: 100%;
		margin: auto;
		height: 45px;
		padding-top: 5px;
		font-size: 14px;
	}
	.padding-xl .searchStore input{
		flex: 1;
		height: 35px;
		border: 1px solid #CCC;
		border-top-left-radius: 5px;
		border-bottom-left-radius: 5px;
		padding-left: 10px;
	}
	.padding-xl{
		padding: 10px;
		
	}
	.padding-xl .searchStore button{
		font-size: 14px;
		color:#FFF;
		padding-left: 8px;
		padding-right: 8px;
		height: 35px;
		line-height: 35px;
		border-radius: 0px;
		border-top-right-radius: 5px;
		border-bottom-right-radius: 5px;
		text-align: center;
		background: rgba(124, 170, 216, 1);
	}
	.padding-xl .StoreListWrap{
		height: 250px;
		overflow: auto;
	}
	.padding-xl .StoreListWrap view{
		height: 40px;
		line-height: 40px;
		text-align: left;
		border-bottom: 1px solid #CCC;
		padding-left: 8px;
	}
	.cu-form-group{
		padding: 1px 10px !important;
		height: 40px;
		line-height: 40px;
		min-height: 40px;
	}
	.cu-form-group .priceSear{
		width: 50px;
		padding: 0px;
		height: 28px;
		line-height: 28px;
		font-size: 12px;
		border-radius: 5px;
		background: rgba(124, 170, 216, 1) !important;
		color:#FFF;
	}
</style>
